package bluetooth;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.widget.Toast;

import com.ecdc.soexample.App;

public class MyServiceConnection implements ServiceConnection {
    public static BluetoothLeService mBluetoothLeService;
    private String bluetoothDeviceAddress;

    public MyServiceConnection(String bluetoothDeviceAddress){
        this.bluetoothDeviceAddress=bluetoothDeviceAddress;
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
        if (!mBluetoothLeService.initialize()) {
            Toast.makeText(App.context, "蓝牙初始化失败", Toast.LENGTH_SHORT).show();
        } else {
            mBluetoothLeService.connect(bluetoothDeviceAddress);
        }

    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mBluetoothLeService = null;
    }

    public void connectBluetooth(String bluetoothDeviceAddress){
        if(mBluetoothLeService==null){
            Toast.makeText(App.context,"蓝牙服务未初始化,请退出软件重新连接",Toast.LENGTH_LONG).show();
        }else {
            mBluetoothLeService.connect(bluetoothDeviceAddress);
        }

    }
}
